//
//  HomeABargainViewController.m
//  lmq
//
//  Created by 马田成 on 2017/3/14.
//  Copyright © 2017年 yuanxin. All rights reserved.
//

#import "HomeABargainViewController.h"
#import "HomeABargainCollectionViewCell.h"
#import "HomeABargainModel.h"
#import "detailsViewController.h"


@interface HomeABargainViewController ()<UICollectionViewDelegate, UICollectionViewDataSource>
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSMutableArray *dataArr;


@end

@implementation HomeABargainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self creationDate];
    [self configLayout];
    
    // Do any additional setup after loading the view.
}
- (void)configLayout{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumLineSpacing = 10;
    layout.minimumInteritemSpacing = 10;
    layout.itemSize = CGSizeMake((DEF_SCREEN_WIDTH - 30) / 2, 280);
    //直接布局 上下左右
    layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, DEF_SCREEN_WIDTH, DEF_SCREEN_HEIGHT - 64 - 49 - 42) collectionViewLayout:layout];
    [self.view addSubview:self.collectionView];
    //
    [self.collectionView layoutIfNeeded];
    //    self.automaticallyAdjustsScrollViewInsets = NO;
    self.collectionView.pagingEnabled = NO;
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    

    [self.collectionView registerNib:[UINib nibWithNibName:@"HomeABargainCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"HomeABargainCollectionViewCell"];
    
    self.collectionView.showsHorizontalScrollIndicator = NO;
    
    //    self.collectionView.bounces = NO;
    self.collectionView.backgroundColor = [UIColor whiteColor];
}
- (void)creationDate{
    
    _dataArr = [[NSMutableArray alloc]init];
//    NSMutableDictionary *productParams=[[NSMutableDictionary alloc]init];
//    NSString *tokenStr=[PublicFunction getConfigWithKey:@"token"];
//    NSString *memberId = [PublicFunction getConfigWithKey:@"uid"];
//    [productParams setValue:tokenStr forKey:@"token"];
//    [productParams setValue:@"1" forKey:@"type"];
//    [productParams setValue:memberId forKey:@"member_id"];
    
    [[MNetworkHandler sharedInstance]conURL:Home_ABargain
                                networkType:NetWorkPOST
                                     params:nil
                                   delegate:nil
                                    showHUD:NO
                               successBlock:^(id returnData) {
                                   if ([[returnData objectForKey:@"code"]intValue]==1) {
                                       
                                       NSDictionary *dict1=[returnData objectForKey:@"data"];
                                       ;
                                       
                                       
                                       
                                       for (NSDictionary *dic in dict1 ) {
                                           HomeABargainModel *model = [[HomeABargainModel alloc]init];
                                           [model setValuesForKeysWithDictionary:dic];
                                           [_dataArr addObject:model];
                                       }
                                       
                                       
                                       [self.collectionView reloadData];
                                   }
                                   else if ([[returnData objectForKey:@"code"]intValue]==-1){
                                       [PublicFunction showAlert:nil message:[returnData objectForKey:@"msg"] duration:5.0f];
                                   }
                               } failureBlock:^(NSError *error) {
                                   NSLog(@"%@",error);
                                   [PublicFunction showAlert:nil message:@"没有连接网络" duration:10.0f];
                               }];
    
}

//类别
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    
    return 1;
    
}
//数量
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    
    return self.dataArr.count;
    
    
}
//内容

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    HomeABargainCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HomeABargainCollectionViewCell" forIndexPath:indexPath];

    HomeABargainModel *model = self.dataArr[indexPath.row];
    cell.modelOfABargain = model;
    
    
    
    
    return cell;
    
    
    
}
//点击
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    detailsViewController *detailsVC=[[detailsViewController alloc]init];
    detailsVC.hidesBottomBarWhenPushed=YES;
    
    HomeABargainModel *model = self.dataArr[indexPath.row];
    detailsVC.myStr = model.shop_id;
    
    
    [self.navigationController pushViewController:detailsVC animated:YES];

    
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
